package com.yjhb.android.sbz.manager;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSFederationCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSFederationToken;
import com.yjhb.android.sbz.v2.mvp.presenter.OSSPresenter;
import com.jojo.android.zxlib.base.mvp.BaseView;
import com.jojo.android.zxlib.tool.ZxLogTool;

/**
 * 创建者 zx
 * 创建时间 2021/7/12 9:35
 */
public class OSSManager {

    //https://shangbangzhuan.oss-cn-beijing.aliyuncs.com/temp/16260833496821338330271403014479.jpeg
    public static final String BUCKET_NAME = "shangbangzhuan";
    public static final String POINT = "oss-cn-beijing.aliyuncs.com";

    private static final String endpoint = String.format("https://%s",POINT);

    private static OSSManager instance;
    private Context context;
    private OSS oss;
    private OSSPresenter presenter;

    public static void init(Context context) {
        instance = new OSSManager(context);
    }

    public static OSSManager getInstance() {
        if (instance == null) {
            throw new RuntimeException("OSSManager ont init!!!");
        }
        return instance;
    }

    private OSSManager(Context context) {
        this.context = context;
        initOSS();
    }

    /**
     * 初始化OSS客户端
     */
    private void initOSS() {
        OSSCredentialProvider provider = new OSSFederationCredentialProvider() {
            @Override
            public OSSFederationToken getFederationToken() throws ClientException {
                try {
                    if (presenter == null) {
                        presenter = new OSSPresenter(new BaseView() {
                            @Override
                            public void onFailure(int code, String msg) {
                                ZxLogTool.err(new Throwable("OSS token 获取失败，code:" + code + " msg:" + msg));
                            }
                        });
                    }
                    JSONObject jsonObjs = JSON.parseObject(presenter.getToken()).getJSONObject("credentials");
                    String ak = jsonObjs.getString("accessKeyId");
                    String sk = jsonObjs.getString("accessKeySecret");
                    String token = jsonObjs.getString("securityToken");
                    String expiration = jsonObjs.getString("expiration");
                    return new OSSFederationToken(ak, sk, token, expiration);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        };
        oss = new OSSClient(context, endpoint, provider);
    }


    /**
     * 获取OSS客户端
     * @return
     */
    public OSS getOss(){
        return oss;
    }

    /**
     * 拼接上传成功的地址
     * @param dir
     * @param fileName
     * @return
     */
    public static String getUrl(String dir,String fileName){
        return String.format("http://chemishequ.com/%s/%s",
//                OSSManager.BUCKET_NAME,
//                OSSManager.POINT,
                dir,
                fileName
                );
    }

}
